import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import member from "@src/store/member";
import base from "@src/store/base";
import {getMemberMineInfo} from "@src/api/member";
import router from "@src/router";

Component({
  options: {
    addGlobalClass: true
  },
  behaviors: [storeBindingsBehavior, router.routerBehavior],
  storeBindings: [{
    store: member,
    fields: ['isMember', 'memberName', 'categoryName', 'categoryImage', 'wxName', 'cardNo'],
    actions: ['getBaseInfo'],
  }, {
    store: base,
    fields: ['businessName', 'businessLogo', 'features', 'perms', 'cardFields'],
  }],
  properties: {
    login: {
      type: Boolean,
      value: false
    },
    cardImg: String
  },
  data: {
    accountInfo: null,
  },
  lifetimes: {
    attached() {
      if (this.data.isMember) {
        this.fetchData();
      }
    }
  },
  observers: {
    'isMember': function (isMember) {
      if (isMember) {
        this.fetchData();
      }
    }
  },
  methods: {
    fetchData() {
      getMemberMineInfo().then(res => {
        this.setData({"accountInfo": res.data})
      })
    },
    handleReqLogin() {
      this.triggerEvent('login')
    },
    handleViewFeature(e) {
      if (this.data.login) {
        let item = e.currentTarget.dataset.item;
        router.push({path: item.entrancePath})
      } else {
        this.handleReqLogin()
      }
    }
  }
})
